package com.example.reference_book.controller;

import com.example.reference_book.entity.Book;
import com.example.reference_book.entity.Course;
import com.example.reference_book.service.BookService;
import com.example.reference_book.util.BookSearchUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 书籍表(Book)表控制层
 *
 * @author makejava
 * @since 2022-06-07 23:05:17
 */
@RestController
@RequestMapping("book")
public class BookController {
    /**
     * 服务对象
     */
    @Resource
    private BookService bookService;

    private BookSearchUtil bookSearchUtil = BookSearchUtil.getBookSearchUtil();

    @Resource
    private SearchBookUtil searchBookUtil=SearchBookUtil.getSearchBookUtil();
    /**
     * 分页查询
     *
     * @param book 筛选条件
     * @return 查询结果
     */
    @PostMapping("/select")
    public ResponseEntity<List<Book>> queryByPage(Book book) {
        List<Book> bookList = this.bookService.queryByPage(book);
        bookSearchUtil.init(bookList);
        return ResponseEntity.ok(bookList);
    }

    @GetMapping("/searchByKey/{type}/{key}")
    public ResponseEntity<List<Book>> searchByKey(@PathVariable("type") Integer type,@PathVariable("key") String key){
        return ResponseEntity.ok(this.bookSearchUtil.searchByKey(type,key));
    }

    @GetMapping("/{id}")
    public ResponseEntity<Book> queryId(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.bookService.queryId(id));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectById/{id}")
    public ResponseEntity<Book> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.bookService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param book 实体
     * @return 新增结果
     */
    @PostMapping("/add")
    public ResponseEntity<Book> add(Book book) {
        return ResponseEntity.ok(this.bookService.insert(book));
    }


    @GetMapping("/searchBook/{type}/{key}")
    public List<Book> searchByKey(@PathVariable("type") String type, @PathVariable("key") String key){
        return this.searchBookUtil.search(type,key);
    }

    @ResponseBody
    public ResponseEntity<List<Book>> queryByName(String type,String key) {
//        System.out.println("search---2");
        return ResponseEntity.ok(this.bookService.queryByName(type,key));
    }

    @ResponseBody
    public ResponseEntity<List<Book>> queryByTag(String type,String key) {
//        System.out.println("search---2");
        return ResponseEntity.ok(this.bookService.queryByTag(type,key));
    }

    /**
     * 编辑数据
     *
     * @param book 实体
     * @return 编辑结果
     */
    @PostMapping("/update")
    public ResponseEntity<Book> edit(Book book) {
        return ResponseEntity.ok(this.bookService.update(book));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @PostMapping("/delete")
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.bookService.deleteById(id));
    }


    /*
    hmj书籍推送使用
     */

    @PostMapping("/deleteAll")
    public ResponseEntity<Boolean> deleteAllById(int[] ids) {
        for(int i=0;i<ids.length;i++){
            this.bookService.deleteById(ids[i]);
        }
        return ResponseEntity.ok(true);
    }
}

